// pages/categories/categories.js
let utils = require("../../utils/utils.js");
const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		notice: [],
		autoplay: true,
		/* 轮播图自动切换 */
		interval: 5000,
		/* 切换间隔时间 */
		duration: 1000,
		/* 起始切换时间 */
		circular: true,
		/* 是否采用衔接滑动 */
		vertical: true,
		/* 滑动方向是否为纵向*/
		navIndex: 0,
		curIndex: 0,
		toView: "new",
		indicatorDots: true,
		num: 0,
		totalprice: 0, //产品总价格
		settlementgoods: [], //结算中心所有产品的属性
		emptystatus: false,
		cartcase: true,
		goodslist: [],
		/* 保存显示物品详情 */
		goodsdetailcase: true,
		buystatus:false/* 判断是否跳转到确认订单页面 */
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let that=this;
		that.getopendid()//设置缓存保存个人信息及获取openid
		let url = '/index/list';
		that.pageload(url); ///* 登录加载首页数据 */
		that.banner();/* 轮播图展示 */
		that.notice();/* 公告展示 */
	},
	
	/* 登录获取code/openid/userid数据 */
	getopendid(e){
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		if (!userInfo) {
			let userInfo = {
				openId: '',
				jifen: '',
				nickName: '',
				avatarUrl: '',
				headerValue: '',
				userId: '',
				balance:''
			}
			wx.setStorageSync('userInfo', userInfo);
			utils._login(res=>{
				let userInfo = wx.getStorageSync('userInfo');
				let jsonuserinfo = res.data.data;
				userInfo.openId = jsonuserinfo.openId;
				userInfo.headerValue = jsonuserinfo.sessionValue;
				userInfo.userId = jsonuserinfo.userId;
				wx.setStorageSync('userInfo', userInfo);
			});
		}else {
			console.log(1);
			console.log(userInfo);
			
			if (userInfo.openId == '' || userInfo.headerValue == '' || userInfo.userId == '') {
				console.log(2);
				utils._login(res=>{
					console.log(res);
					let userInfo = wx.getStorageSync('userInfo');
					let jsonuserinfo = res.data.data;
					userInfo.headerValue = jsonuserinfo.sessionValue;
					wx.setStorageSync('userInfo', userInfo);
					/* if (jsonuserinfo.requireBind != true) {
						userInfo.userId = jsonuserinfo.userId;
						wx.setStorageSync('userInfo', userInfo);
					} */
				});
			}
		}
	},
	/* 首页加载数据 */
	pageload(_url) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let openId = userInfo.openId;
		let headerValue = userInfo.headerValue;
		/* 获取首页数据 */
		let base_url = app.globalData.baseurl;
		//console.log(_url);
		
		//request(userInfo, url, methods, data, success) 
		//console.log(openId,sessionValue);
		utils.request(userInfo,_url,'get','',res=>{
			if (res.data.status == 200) {
				let data = res.data.data;
				let nav_list = data.type;
				let cate_nav_list = data.list;
				let goods_list = data.details;
				for (let i = 0; i < goods_list.length; i++) {
					let shuxin = Object.getOwnPropertyNames(goods_list[i]).length;
				}
				let settlementgoods = wx.getStorageSync('settlementgoods');
				for (var i = 0; i < goods_list.length; i++) {
					goods_list[i].imageUrl = base_url + goods_list[i].imageUrl;
					goods_list[i].pushicon = false; /* 控制下拉图标指向的 */
					goods_list[i].liststutas = false; /* 控制下拉图标指向的 */
					goods_list[i].addminus = false; /* 控制下拉图标指向的 */
					if (goods_list[i].discount == 0) {
						goods_list[i].discount = '非折扣';
					} else {
						goods_list[i].discount = '折扣';
					}
				}
				/* 控制下拉显示隐藏及显示产品数量及缓存里的产品数量及属性值 */
				that.showdome(settlementgoods, goods_list);
				that.data.goodslist = goods_list;
				// 计算总价
				that.totalprice();
				that.setData({
					nav_list: nav_list,
					cate_nav_list: cate_nav_list
				})
			}
		})
	},
	// 轮播图
	banner(e) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let _url ='/index/query/carousel';
		let base_url=app.globalData.baseurl;
		utils.request(userInfo,_url,'get','',res=>{
			if(res.data.status==200){
				let index_slides = res.data.data;
				if(index_slides.length>0){
					for (let i = 0; i < index_slides.length; i++) {
						index_slides[i].imageUrl = base_url + index_slides[i].imageUrl;
					}
					that.setData({
						index_slides: index_slides
					})
				}
			}
		})
	},
	//公告
	notice(e) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = '/index/query/notice';
		utils.request(userInfo,_url,'get','',res=>{
			if(res.data.status==200){
				let notice = res.data.data;
				that.setData({
					notice
				})
			}
		})
	},
	// 公告列表
	noticeList: function(e) {
		wx.navigateTo({
			url: '../noticeList/noticeList'
		})
	},
	/* 点击大类 */
	navdata(e) {
		let that = this;
		const navIndex = e.currentTarget.dataset.index ? e.currentTarget.dataset.index : 0;
		let vid = e.currentTarget.dataset.id;
		let _url = '/index/queryByType?id=' + vid;
		that.pageload(_url); //加载大类数据
		this.setData({
			navIndex,
			curIndex: 0
		});
	},
	/* 点击分类列表 */
	switchCategory(e) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let openId = userInfo.openId;
		let sessionValue = userInfo.headerValue;
		let id = e.currentTarget.dataset.id;
		let base_url = app.globalData.baseurl;
		let _url = '/index/queryByList?id=' + id;
		utils.request(userInfo,_url,'get','',res=>{
			console.log(res);
			if(res.data.status==200){
				let goods_list = res.data.data;
				that.data.goods_list = res.data.data;
				let settlementgoods = wx.getStorageSync('settlementgoods');
				for (var i = 0; i < goods_list.length; i++) {
					goods_list[i].imageUrl = base_url + goods_list[i].imageUrl;
					goods_list[i].pushicon = false; /* 控制下拉图标指向的 */
					goods_list[i].liststutas = false; /* 控制下拉图标指向的 */
					goods_list[i].addminus = false; /* 控制下拉图标指向的 */
					if (goods_list[i].discount == 0) {
						goods_list[i].discount = '非折扣';
					} else {
						goods_list[i].discount = '折扣';
					}
				}
				that.showdome(settlementgoods, goods_list);
				that.data.goodslist = goods_list;
			}
		})
		const curIndex = e.currentTarget.dataset.index ? e.currentTarget.dataset.index : 0;
		this.setData({
			toView: e.currentTarget.dataset.id,
			curIndex
		});
	},
	/*点击下拉图标显示详情*/
	pushla(e) {
		let that = this;

		let goods_list = this.data.goods_list;
		let liststutas = e.currentTarget.dataset.liststutas;
		let index = e.currentTarget.dataset.index;
		let id = e.currentTarget.dataset.id;
		/* 显示缓存结算数量 */
		let settlementgoods = wx.getStorageSync('settlementgoods');
		that.showdome(settlementgoods, goods_list)
		if (liststutas == true) {
			/* 隐藏所有下拉框 */
			goods_list[index].liststutas = false;
			goods_list[index].pushicon = false;

		} else {
			/*隐藏所有下拉框 */
			goods_list[index].liststutas = true;
			goods_list[index].pushicon = true;
		}
		that.setData({
			goods_list: goods_list
		});
	},
	/* 点击图片查看详情 */
	goodsdetail(e) {
		let that = this;
		let index = e.currentTarget.dataset.index;
		let goodsdetail = that.data.goods_list[index];
		that.setData({
			goodsdetailcase: false,
			goodsdetail: goodsdetail
		})
	},
	/* 增加产品详情数量 */
	gdetail_addnum(e) {
		let that = this;
		let goodlist = that.data.goods_list;
		let goodsdetail = that.data.goodsdetail;
		goodsdetail.num++;
		let _goodsdetail = [];
		_goodsdetail.push(goodsdetail);
		that.showdome(_goodsdetail, goodlist);
		that.storagegoodsinfo(goodsdetail); /* 保存到缓存 */
		that.setData({
			goodsdetail: goodsdetail
		})

	},
	gdetail_minusnum(e) {
		let that = this;
		let goodlist = that.data.goods_list;
		let goodsdetail = that.data.goodsdetail;
		goodsdetail.num--;
		if (goodsdetail.num == 0) {
			goodsdetail.addminus = false;
		}
		let _goodsdetail = [];
		_goodsdetail.push(goodsdetail);
		that.showdome(_goodsdetail, goodlist);
		that.storagegoodsinfo(goodsdetail); /* 保存到缓存 */
		that.setData({
			goodsdetail: goodsdetail
		})

	},
	/* 减去购物车某个产品数量 */
	cart_minusnum(e) {
		let that = this;
		let cart_list = this.data.cart_list;
		let goodslist = that.data.goodslist; /* 列表显示数据 */
		let settlementgoods = wx.getStorageSync('settlementgoods');
		let index = e.currentTarget.dataset.index;
		let add = e.currentTarget.dataset.add;
		cart_list[index].addminus = true;
		cart_list[index].num--;
		let currengoodlist = cart_list[index];
		that.storagegoodsinfo(currengoodlist); /* 保存到缓存 */
		settlementgoods = cart_list;
		that.showdome(settlementgoods, goodslist);
		if (cart_list[index].num == 0) {
			cart_list.splice(index, 1);
		}
		that.setData({
			cart_list: cart_list,
		});
	},
	/* 添加购物车某个产品数量 */
	cart_addnum(e) {
		let that = this;
		let goodslist = that.data.goodslist; /* 列表显示数据 */
		let cart_list = this.data.cart_list;
		let settlementgoods = wx.getStorageSync('settlementgoods');
		let index = e.currentTarget.dataset.index;
		let minus = e.currentTarget.dataset.minus;
		cart_list[index].num++;
		let currengoodlist = cart_list[index];
		that.storagegoodsinfo(currengoodlist);
		settlementgoods = cart_list;
		that.showdome(settlementgoods, goodslist);
		that.storagegoodsinfo(currengoodlist); /*  */
		that.setData({
			cart_list: cart_list
		});
	},
	/* 加入产品数量 */
	addnum(e) {
		let that = this;
		let totalprice = this.data.totalprice;
		let goods_list = this.data.goods_list;
		let index = e.currentTarget.dataset.index;
		let add = e.currentTarget.dataset.add;
		goods_list[index].addminus = true;
		goods_list[index].num++;
		let currengoodlist = goods_list[index];
		that.storagegoodsinfo(currengoodlist);
		that.setData({
			goods_list: goods_list,

		});
	},
	/* 减少产品数量 */
	minusnum(e) {
		let that = this;
		let goods_list = this.data.goods_list;
		let index = e.currentTarget.dataset.index;
		let minus = e.currentTarget.dataset.minus;
		goods_list[index].num--;
		if (goods_list[index].num == 0) {
			goods_list[index].addminus = false;
		}
		let currengoodlist = goods_list[index];
		that.storagegoodsinfo(currengoodlist); /* 改变缓存中产品数量及减号状态 */
		that.setData({
			goods_list: goods_list
		});
	},
	/* 清空购物车 */
	emptycart(e) {
		let that = this;
		let emptycartlist = wx.getStorageSync('settlementgoods');
		for (let i = 0; i < emptycartlist.length; i++) {
			emptycartlist[i].num = 0;
			emptycartlist[i].addminus = false;
			emptycartlist[i].liststutas = false;
		}
		let goodslist = that.data.goodslist;
		that.showdome(emptycartlist, goodslist); /* 第一个参数是缓存数据 */
		wx.removeStorageSync('settlementgoods');
		that.setData({
			cartcase: true,
			totalnum: 0,
			totalprice: 0,
			emptystatus: true
		})
	},
	/*去结算 */
	confirmorder(e) {
		let that = this;
		let buystatus = that.data.buystatus;
		let userInfo = wx.getStorageSync('userInfo');
		/* 请选购产品 */
		let orderlist = wx.getStorageSync('settlementgoods');
		if (!orderlist) {
			wx.showModal({
				title: "提示",
				content: "请选购产品。",
				showCancel: false
			});
			return false;
		}
		
		if(!userInfo.userId){
			wx.redirectTo({
				'url':'../login/login'
			})
			return false;
		}
		//that.login();
		
		for (let i = 0; i < orderlist.length; i++) {
			delete orderlist[i].pushicon; /* 清除自己加的对象属性 */
			if (orderlist[i].discount == '非折扣') {
				orderlist[i].discount = 0;
			} else {
				orderlist[i].discount = 1;
			}
		}
		
		let orderAmount = this.data.totalprice;
		let _url ='/order/create';
		/* JSON.stringify */
		let data = {
			orderList: JSON.stringify(orderlist),
			openId: userInfo.openId,
			userId: userInfo.userId,
			orderAmount: orderAmount
		}
		wx.showLoading({
			title: '客官不要急~',
			mask: true
		})
		utils.request(userInfo,_url,'post',data,res=>{
			let orderlist = res.data.data;
			if (res.data.status == 200) {
				let addresdirect=true;
				wx.setStorageSync('addresdirect', addresdirect);
				wx.setStorageSync('orderlist', orderlist);
				that.data.buystatus = true; /* 判断是否是跳转到购买页面 */
				setTimeout(function(){
					that.emptycart();
				},4000)
				wx.navigateTo({
					url: '../confirmorder/confirmorder'
				});
			}else if(res.data.status==501){
				utils.login(res => {
					console.log(res);
					let jsonuserinfo = res.data.data;
					userInfo.headerValue = jsonuserinfo.sessionValue;
					wx.setStorageSync('userInfo', userInfo);
					that.confirmorder();
				});
			}
		})

	},
	mine(e) {
		let that = this;
		let mine = 1; /* 判断绑定时登录页面 1是myorder页面 2是我的页面3是确认订单页面 */
		let userInfo = wx.getStorageSync('userInfo');
		if(!userInfo.userId||userInfo.userId==undefined||!userInfo.avatarUrl||userInfo.avatarUrl==undefined){
			wx.navigateTo({
				url: '../login/login'
			})
		}else{
			wx.navigateTo({
				url: '../mine/mine'
			})
		}
	},
	myorder(e) {
		let that = this;
		let mine = 2; /* 判断绑定时登录页面 1是myorder页面 2是我的页面3是确认订单页面 */
		let userInfo = wx.getStorageSync('userInfo');
		if(!userInfo.userId||userInfo.avatarUrl==''){
			wx.navigateTo({
				url: '../login/login'
			})
		}else{
			wx.navigateTo({
				url: '../myorder/myorder?type=0'
			})
		}
		
	},
	/*  打开购物车 */
	provercart(e) {
		let that = this;
		let cart_list = wx.getStorageSync('settlementgoods');
		if (!cart_list) {
			return false;
		}
		let cartcase = that.data.cartcase;
		if (cartcase == true) {
			that.data.cartcase = false;
			that.setData({
				cartcase: that.data.cartcase,
				cart_list: cart_list
			})
		} else {
			that.data.cartcase = true;
			that.setData({
				cartcase: that.data.cartcase
			})
		}

	},
	/* 关闭购物车 */
	closecartcase(e){
		this.setData({
			cartcase:true
		})
		
	},
	/* 关闭产品详情 */
	closegoodsdetail(e) {
		let that = this;
		let  id=e.target.dataset.id;
		if(id==1){
			that.setData({
				goodsdetailcase: true
			})
		}
		
	},
	/* 计算总价 */
	totalprice(e) {
		let that = this;
		let settlementgoods = wx.getStorageSync('settlementgoods');
		let totalprice = 0;
		let totalnum = 0;
		for (let i = 0; i < settlementgoods.length; i++) {
			totalnum += settlementgoods[i].num;
			if (settlementgoods[i].discount == '非折扣') {
				totalprice += settlementgoods[i].num * settlementgoods[i].price;
			} else {
				totalprice += settlementgoods[i].num * settlementgoods[i].discountPrice;
			}
		}
		totalprice = totalprice.toFixed(2);
		let emptystatus = true;
		if (totalprice != 0) {
			emptystatus = false;
		}
		that.setData({
			totalprice: totalprice,
			totalnum: totalnum,
			emptystatus: emptystatus
		})
	},
	/* 控制下拉和减号及折扣显示、隐藏 及数据列表数量改变*/
	showdome(settlementgoods, goods_list) {
		let that = this;
		let base_url = app.globalData.baseurl;
		if (settlementgoods.length >= goods_list.length) {
			for (let i = 0; i < settlementgoods.length; i++) {
				for (let j = 0; j < goods_list.length; j++) {
					if (settlementgoods[i].id == goods_list[j].id) {
						goods_list[j].addminus = settlementgoods[i].addminus;
						goods_list[j].num = settlementgoods[i].num;
						goods_list[j].liststutas = settlementgoods[i].liststutas;
					}
				}
			}
		} else {
			for (let i = 0; i < goods_list.length; i++) {
				for (let j = 0; j < settlementgoods.length; j++) {
					if (goods_list[i].id == settlementgoods[j].id) {
						goods_list[i].addminus = settlementgoods[j].addminus;
						goods_list[i].num = settlementgoods[j].num;
						goods_list[i].liststutas = settlementgoods[j].liststutas;
					}
				}
			}
		}
		wx.hideLoading();/* 隐藏加载框 */
		that.setData({
			goods_list: goods_list
		})
	},
	/* 加减产品加入购物车函数及删除购物车产品 */
	storagegoodsinfo(currengoodlist) {
		/* 把产品加入结算中心 */
		let that = this;
		if (currengoodlist.num < 1) {
			let settlementgoods = wx.getStorageSync('settlementgoods');
			currengoodlist.liststutas = false;
			currengoodlist.addminus = false;
			for (var i = 0; i < settlementgoods.length; i++) {
				if (settlementgoods[i].id == currengoodlist.id) {
					settlementgoods.splice(i, 1);
					wx.setStorageSync('settlementgoods', settlementgoods);
					break;
				}
			}
		} else {
			currengoodlist.liststutas = true;
			currengoodlist.addminus = true;
			let settlementgoods = wx.getStorageSync('settlementgoods');
			if (!settlementgoods) {
				settlementgoods = [];
				settlementgoods.push(currengoodlist);
				wx.setStorageSync('settlementgoods', settlementgoods);
			} else {
				let settlementgoods = wx.getStorageSync('settlementgoods');
				let arrfalse = false;
				for (var i = 0; i < settlementgoods.length; i++) {
					if (settlementgoods[i].id == currengoodlist.id) {
						settlementgoods.splice(i, 1, currengoodlist); /* 替换数组中的相同项 */
						wx.setStorageSync('settlementgoods', settlementgoods);
						arrfalse = true;
						break;
					}
				}
				if (arrfalse == false) {
					settlementgoods.push(currengoodlist);
					wx.setStorageSync('settlementgoods', settlementgoods);
				}
			}
		}
		/* 计算总价 */
		that.totalprice();
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		
	},
	
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
		
	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
